﻿using System;

namespace Game
{
    class Credits
    {
        private class JumpingEnemy : Player
        {
            private int maxY = 295;
            private bool isFalling = true;
            private bool isJumping = false;
            private int currentJumpFrame = 0;
            
            private int[] jumpSteps = {-10, -10, -8, -8, -6, -6, -4, -2, -1, -1, 0,
                             0, 1, 1, 2, 4, 6, 6, 8, 8, 10, 10 };

            public JumpingEnemy()
            {
                ySpeed = 24;
                xSpeed = 0;
            }

            public void MoveInCredits()
            {
                
                x += xSpeed;
                if ((x > 700) || (x < 50))
                {
                    xSpeed = -xSpeed;

                }
                if(x == 699)
                        LoadImage("data/lemmingLeft0.png");
                else if(x == 51)
                        LoadImage("data/lemming1.png");
                if (isFalling)
                {
                    y += ySpeed;
                    if (y > maxY) 
                    {
                        ySpeed = 0;
                        xSpeed = 12;
                        isFalling = false;
                    }
                }

                
                if (isJumping) 
                {
                    y += jumpSteps[currentJumpFrame];
                    currentJumpFrame++;
                    if (currentJumpFrame >= jumpSteps.Length)
                    {
                        currentJumpFrame = 0;
                        isJumping = false;
                    }
                }

            
            }

            public void Jump()
            {
                if ((!isFalling) && (!isJumping))
                    isJumping = true;
            }
        }


        JumpingEnemy[] myLemming = new JumpingEnemy[6];
        int numLemmings;
        int counter;
        public Credits()
        {
            numLemmings=0;
            counter=0;
            for (int i = 0; i < 5; i++)
            {

                myLemming[i] = new JumpingEnemy();

                myLemming[i].MoveTo(DateTime.Now.Millisecond % 750, 20);

            }
        }

        public void Run()
        {
            Font sans18;
            sans18 = new Font("data/Joystix.ttf", 18);

            do
            {
                Hardware.ClearScreen();
                Hardware.WriteHiddenText(" The game is developed by: ",
                    200, 310,
                    0xCC, 0xCC, 0xCC,
                    sans18);
                Hardware.WriteHiddenText(" Juan Antonio Ripoll ",
                    240, 340,
                    0x0, 0xB0, 0x0,
                    sans18);
                Hardware.WriteHiddenText(" Carlos Miquel ",
                    275, 360,
                    0x40, 0x40, 0xE0,
                    sans18);
                Hardware.WriteHiddenText(" Alejandro Ribeiro ",
                    250, 380,
                    0x12, 0xCC, 0x25,
                    sans18);
                Hardware.WriteHiddenText(" Dani Segura ",
                    285, 400,
                    0x40, 0x40, 0xE0,
                    sans18);
                Hardware.WriteHiddenText(" Vicente Giner ",
                    270, 420,
                    0x12, 0xCC, 0x25,
                    sans18);                
                Hardware.WriteHiddenText(" Emilio Garcia ",
                    271, 440,
                    0x40, 0x40, 0xE0,
                    sans18);
                Hardware.WriteHiddenText(" Saul Sánchez ",
                    275, 460,
                    0x12, 0xCC, 0x25,
                    sans18);
                Hardware.WriteHiddenText("Hector Nieves ",
                    283, 480,
                    0x40, 0x40, 0xE0,
                    sans18);
                Hardware.WriteHiddenText(" Víctor Rico ",
                    282, 500,
                    0x12, 0xCC, 0x25,
                    sans18);
                Hardware.WriteHiddenText("Hit ESC to return ",
                    255, 530,
                    0x99, 0x99, 0x99,
                    sans18);

               

                

                for (int j = 0; j < 2; j++)
                {

                    if (counter == 0 && numLemmings < 5)
                    {
                        myLemming[numLemmings].DrawOnHiddenScreen();
                        //Hardware.Pause(5);
                        
                        numLemmings++;

                    }
                    myLemming[numLemmings].Jump();
                    myLemming[numLemmings].MoveInCredits();
                        Hardware.ShowHiddenScreen();
                    

                    if (counter >= 2000000)
                        counter = 0;
                    
                    counter++;
                    myLemming[j].DrawOnHiddenScreen();
                    //Hardware.Pause(5);
                    myLemming[j].MoveInCredits();
                    Hardware.ShowHiddenScreen();

                    myLemming[j].Jump();
                  
                }
                
            
                Hardware.Pause(20);
            }
            while (!Hardware.KeyPressed(Hardware.KEY_ESC));
        }
        }
    }

